package com.leetcode.Offer;

public class JZ58_I {
    public String reverseWords(String s) {
        if (s == null || s.length() == 0) {
            return "";
        }
        String str = s.trim().replaceAll("\\s+", " ");
        String[] split = str.split(" ");
        StringBuilder builder = new StringBuilder();
        for (int i = split.length - 1; i >= 0; i--) {
            if (i > 0) {
                builder.append(split[i]).append(" ");
            } else {
                builder.append(split[i]);
            }
        }

        return builder.toString();
    }

/*    //转置字符
    private String toTranspose(String str) {
        if (Character.isLetter(str.charAt(str.length() - 1))) {//判断最后一个字符是否为字母
            return new StringBuilder(str).reverse().toString();
        } else {
            return new StringBuilder(str.substring(0, str.length() - 1)).reverse().append(str.charAt(str.length() - 1)).toString();
        }
    }*/

    public static void main(String[] args) {
        /*String split = " i love   you  hhh ".trim().replaceAll("\\s+", " ");
        System.out.println(split);*/
        System.out.println(new JZ58_I().reverseWords("  hello  world!  "));

    }
}
